var manager = {};

manager.param = {
		page: 1,
		rows: 10,
		name: null,
		type: "manager"
}

manager.categoryParam = {
		page: 1,
		rows: 10,
		flag: 1
}

manager.categorys = ['write_data', 'send_data', 'draw_data', 'pick_data'];

manager.pageBack = function(num, type) {
	manager.param.page = num;
	manager.getData();
}

manager.pageInit = function(pageNum, page, pageChangeBack) {
	$('#pagination').empty();
    var jqPaginator = $.jqPaginator('#pagination', {
        totalPages: pageNum,
        visiblePages: 5,
        currentPage: page,
        first:'<li class="first" jp-role="first" jp-data="1"><a href="javascript:;">首页</a></li>',
        prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
        next: '<li class="next"><a href="javascript:;">下一页</a></li>',
        last:'<li class="last" jp-role="last" jp-data="100"><a href="javascript:;">尾页</a></li>',
        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
        //onPageChange: function (num, type) {
         //   $('#p1').text(type + '：' + num);
        //}
        onPageChange: pageChangeBack
    });
}

manager.categroyPageInit = function(pageNum, page, pageChangeBack) {
	if (manager.categoryParam.flag == 1) {
		$('#write_pagination').empty();
	    var jqPaginator = $.jqPaginator('#write_pagination', {
	        totalPages: pageNum,
	        visiblePages: 5,
	        currentPage: page,
	        first:'<li class="first" jp-role="first" jp-data="1"><a href="javascript:;">首页</a></li>',
	        prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
	        next: '<li class="next"><a href="javascript:;">下一页</a></li>',
	        last:'<li class="last" jp-role="last" jp-data="100"><a href="javascript:;">尾页</a></li>',
	        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
	        //onPageChange: function (num, type) {
	         //   $('#p1').text(type + '：' + num);
	        //}
	        onPageChange: pageChangeBack
	    });
	} else if (manager.categoryParam.flag == 2) {
		$('#send_pagination').empty();
	    var jqPaginator = $.jqPaginator('#send_pagination', {
	        totalPages: pageNum,
	        visiblePages: 5,
	        currentPage: page,
	        first:'<li class="first" jp-role="first" jp-data="1"><a href="javascript:;">首页</a></li>',
	        prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
	        next: '<li class="next"><a href="javascript:;">下一页</a></li>',
	        last:'<li class="last" jp-role="last" jp-data="100"><a href="javascript:;">尾页</a></li>',
	        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
	        //onPageChange: function (num, type) {
	         //   $('#p1').text(type + '：' + num);
	        //}
	        onPageChange: pageChangeBack
	    });
	} else if (manager.categoryParam.flag == 3) {
		$('#draw_pagination').empty();
	    var jqPaginator = $.jqPaginator('#draw_pagination', {
	        totalPages: pageNum,
	        visiblePages: 5,
	        currentPage: page,
	        first:'<li class="first" jp-role="first" jp-data="1"><a href="javascript:;">首页</a></li>',
	        prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
	        next: '<li class="next"><a href="javascript:;">下一页</a></li>',
	        last:'<li class="last" jp-role="last" jp-data="100"><a href="javascript:;">尾页</a></li>',
	        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
	        //onPageChange: function (num, type) {
	         //   $('#p1').text(type + '：' + num);
	        //}
	        onPageChange: pageChangeBack
	    });
	} else if (manager.categoryParam.flag == 4) {
		$('#pick_pagination').empty();
	    var jqPaginator = $.jqPaginator('#pick_pagination', {
	        totalPages: pageNum,
	        visiblePages: 5,
	        currentPage: page,
	        first:'<li class="first" jp-role="first" jp-data="1"><a href="javascript:;">首页</a></li>',
	        prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
	        next: '<li class="next"><a href="javascript:;">下一页</a></li>',
	        last:'<li class="last" jp-role="last" jp-data="100"><a href="javascript:;">尾页</a></li>',
	        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
	        //onPageChange: function (num, type) {
	         //   $('#p1').text(type + '：' + num);
	        //}
	        onPageChange: pageChangeBack
	    });
	}
	
}

manager.getData = function() {
	var url = "../managerInfo/findManagerInfoList.action";
	if (manager.param.type == "category") {
		url = "";
		$('#spread_category_list .data_tr').remove();
	} else {
		$('#spread_manager_list .data_tr').remove();
	}
	var req = new common.Request();
	var reqData = {};
	for (var key in manager.param) {
		if (manager.param[key] != null) {
			reqData[key] = manager.param[key];
		}
	}
	req.sendMethodRequest("get", url, reqData, function(e){
		if (e != null && e.isSuccess) {
			var spread_list = $('#spread_manager_list');
			if (manager.param.type == "category") {
				spread_list = $('#spread_category_list');
			}
			for (var i = 0; i < e.info.length; i ++) {
				(function(i){
					var info = e.info[i];
					var emptyTr = $('<tr></tr>').addClass('data_tr').appendTo(spread_list);
					var emptyTd1 = $('<td></td>').appendTo(emptyTr);
					var emptyTd2 = $('<td></td>').appendTo(emptyTr);
					var emptyTd3 = $('<td></td>').appendTo(emptyTr);
					var emptyTd4 = $('<td></td>').appendTo(emptyTr);
					var emptyTd5 = $('<td></td>').appendTo(emptyTr);
					var emptyTd6 = $('<td></td>').appendTo(emptyTr);
					var emptyTd7 = $('<td></td>').appendTo(emptyTr);
					var dataTr = $('<tr></tr>').addClass('data_tr').appendTo(spread_list);
					var dataTd1 = $('<td></td>').html(info.name == null ? "" : info.name).appendTo(dataTr);
					var dataTd2 = $('<td></td>').html(info.companyName == null ? "" : info.companyName).appendTo(dataTr);
					var dataTd3 = $('<td></td>').html(info.realName == null ? "" : info.realName).appendTo(dataTr);
					var createTime = "";
					if (info.createTime != null) {
						var cateDate = new Date(info.createTime);
						createTime = cateDate.getFullYear()+"-"+(cateDate.getMonth()+1)+"-"+cateDate.getDate();
					}
					var dataTd4 = $('<td></td>').html(createTime).appendTo(dataTr);
					var lastLoginTime = "";
					if (info.lastLoginTime != null) {
						var cateDate = new Date(info.createTime);
						lastLoginTime = cateDate.getFullYear()+"-"+(cateDate.getMonth()+1)+"-"+cateDate.getDate();
					}
					var dataTd5 = $('<td></td>').html(lastLoginTime).appendTo(dataTr);
					var dataTd6 = $('<td></td>').appendTo(dataTr);
					var chaxun = $('<a></a>').html("记录查询").appendTo(dataTd6).click(function() {
						
					});
					var dataTd7 = $('<td></td>').appendTo(dataTr);
					var ziliao = $('<a></a>').html("编辑 /").appendTo(dataTd7).click(function() {
						
					});
					var update = $('<a></a>').html(" 删除 /").appendTo(dataTd7).click(function() {
						
					});
					var renwen = $('<a></a>').html(" 修改密码 ").appendTo(dataTd7).click(function() {
						
					});
				})(i);
			}
			var init = false;
			manager.pageInit(e.sum, manager.param.page, function(num, type){
				manager.param.page = num;
				if (init) {
					manager.getData();
				} else {
					init = true;
				}
			});
		}
	}, function(e) {
		
	});
}

manager.search = function() {
	$('#search_button').click(function(){
		var name_search = $('#name_search').val();
		if (name_search != null && common.string.Trim(name_search) != "") {
			manager.param.name = name_search;
		} else {
			manager.param.name = null;
		}
		manager.getData();
	});
}

manager.categoryPage = function(num, type) {
	manager.categoryParam.page = num;
	manager.getCategoryData();
}

manager.getCategoryData = function() {
	var url = "../category/findCategoryInfo.action";
	var reqData = {};
	for (var key in manager.categoryParam) {
		reqData[key] = manager.categoryParam[key];
	} 
	if (manager.categoryParam.flag == 1) {
		$('#spread_write_list .data_tr').remove();
	} else if (manager.categoryParam.flag == 2) {
		$('#spread_send_list .data_tr').remove();
	} else if (manager.categoryParam.flag == 3) {
		$('#spread_draw_list .data_tr').remove();
	} else if (manager.categoryParam.flag == 4) {
		$('#spread_pick_list .data_tr').remove();
	}
	var req = new common.Request();
	req.sendRequest(url, reqData, function(e){
		if (e != null && e.isSuccess) {
			if (manager.categoryParam.flag == 1) {
				var root = $('#spread_write_list');
				if (e.info != null && e.info.length > 0) {
					for (var i = 0; i < e.info.length; i ++) {
						(function(i){
							var info = e.info[i];
							var emptyTr = $('<tr></tr>').addClass('data_tr').appendTo(root);
							var emptyTd1 = $('<td></td>').appendTo(emptyTr);
							var emptyTd2 = $('<td></td>').appendTo(emptyTr);
							var emptyTd3 = $('<td></td>').appendTo(emptyTr);
							var emptyTd4 = $('<td></td>').appendTo(emptyTr);
							var emptyTd7 = $('<td></td>').appendTo(emptyTr);
							var dataTr = $('<tr></tr>').addClass('data_tr').appendTo(root);
							var dataTd1 = $('<td></td>').html(info.name == null ? "" : info.name).appendTo(dataTr);
							var dataTd2 = $('<td></td>').html((info.price == null ? 0 : info.price) + "元").appendTo(dataTr);
							var dataTd3 = $('<td></td>').html(info.desc == null ? "" : info.desc).appendTo(dataTr);
							var dataTd4 = $('<td></td>').html(info.keyword == null ? "" : info.keyword).appendTo(dataTr);
							var dataTd7 = $('<td></td>').appendTo(dataTr);
							var ziliao = $('<a></a>').html("编辑 /").appendTo(dataTd7).click(function() {
								
							});
							var update = $('<a></a>').html(" 删除 ").appendTo(dataTd7).click(function() {
								
							});
						})(i);
					}
				}
			} else if (manager.categoryParam.flag == 2) {
				var root = $('#spread_send_list');
				if (e.info != null && e.info.length > 0) {
					for (var i = 0; i < e.info.length; i ++) {
						(function(i){
							var info = e.info[i];
							var emptyTr = $('<tr></tr>').addClass('data_tr').appendTo(root);
							var emptyTd1 = $('<td></td>').appendTo(emptyTr);
							var emptyTd2 = $('<td></td>').appendTo(emptyTr);
							var emptyTd3 = $('<td></td>').appendTo(emptyTr);
							var emptyTd7 = $('<td></td>').appendTo(emptyTr);
							var dataTr = $('<tr></tr>').addClass('data_tr').appendTo(root);
							var dataTd1 = $('<td></td>').html(info.cname == null ? "" : info.cname).appendTo(dataTr);
							var dataTd2 = $('<td></td>').html((info.price == null ? 0 : info.price) + "元").appendTo(dataTr);
							var dataTd3 = $('<td></td>').html(info.mname == null ? "" : info.mname).appendTo(dataTr);
							var dataTd7 = $('<td></td>').appendTo(dataTr);
							var ziliao = $('<a></a>').html("编辑 /").appendTo(dataTd7).click(function() {
								
							});
							var update = $('<a></a>').html(" 删除 ").appendTo(dataTd7).click(function() {
								
							});
						})(i);
					}
				}
			} else if (manager.categoryParam.flag == 3) {
				var root = $('#spread_draw_list');
				if (e.info != null && e.info.length > 0) {
					for (var i = 0; i < e.info.length; i ++) {
						(function(i){
							var info = e.info[i];
							var emptyTr = $('<tr></tr>').addClass('data_tr').appendTo(root);
							var emptyTd1 = $('<td></td>').appendTo(emptyTr);
							var emptyTd2 = $('<td></td>').appendTo(emptyTr);
							var emptyTd3 = $('<td></td>').appendTo(emptyTr);
							var emptyTd4 = $('<td></td>').appendTo(emptyTr);
							var emptyTd7 = $('<td></td>').appendTo(emptyTr);
							var dataTr = $('<tr></tr>').addClass('data_tr').appendTo(root);
							var dataTd1 = $('<td></td>').html(info.name == null ? "" : info.name).appendTo(dataTr);
							var dataTd2 = $('<td></td>').html((info.price == null ? 0 : info.price) + "元").appendTo(dataTr);
							var dataTd3 = $('<td></td>').html(info.desc == null ? "" : info.desc).appendTo(dataTr);
							var dataTd4 = $('<td></td>').html(info.keyword == null ? "" : info.keyword).appendTo(dataTr);
							var dataTd7 = $('<td></td>').appendTo(dataTr);
							var ziliao = $('<a></a>').html("编辑 /").appendTo(dataTd7).click(function() {
								
							});
							var update = $('<a></a>').html(" 删除 ").appendTo(dataTd7).click(function() {
								
							});
						})(i);
					}
				}
			} else if (manager.categoryParam.flag == 4) {
				var root = $('#spread_pick_list');
				if (e.info != null && e.info.length > 0) {
					for (var i = 0; i < e.info.length; i ++) {
						(function(i){
							var info = e.info[i];
							var emptyTr = $('<tr></tr>').addClass('data_tr').appendTo(root);
							var emptyTd1 = $('<td></td>').appendTo(emptyTr);
							var emptyTd2 = $('<td></td>').appendTo(emptyTr);
							var emptyTd3 = $('<td></td>').appendTo(emptyTr);
							var emptyTd7 = $('<td></td>').appendTo(emptyTr);
							var dataTr = $('<tr></tr>').addClass('data_tr').appendTo(root);
							var dataTd1 = $('<td></td>').html(info.cname == null ? "" : info.cname).appendTo(dataTr);
							var dataTd2 = $('<td></td>').html((info.price == null ? 0 : info.price) + "元").appendTo(dataTr);
							var dataTd3 = $('<td></td>').html(info.mname == null ? "" : info.mname).appendTo(dataTr);
							var dataTd7 = $('<td></td>').appendTo(dataTr);
							var ziliao = $('<a></a>').html("编辑 /").appendTo(dataTd7).click(function() {
								
							});
							var update = $('<a></a>').html(" 删除 ").appendTo(dataTd7).click(function() {
								
							});
						})(i);
					}
				}
			}
			var init = false;
			manager.categroyPageInit(e.sum, manager.categoryParam.page, function(num, type){
				manager.categoryParam.page = num;
				if (init) {
					manager.getCategoryData();
				} else {
					init = true;
				}
			});
		}
	}, function(e){
		
	});
}

manager.categoryInit = function() {
	$('#navigation ul li').click(function(){
		var ele = $(this);
		var array = $('#navigation ul li');
		for (var i = 0; i < array.length; i ++) {
			$(array[i]).removeClass('select');
		}
		ele.addClass('select');
		var value = ele.attr('value');
		for (var i = 0; i < manager.categorys.length; i++) {
			$('#' + manager.categorys[i]).css('display','none');
		}
		$('#' + manager.categorys[value - 1]).css('display', 'block');
		manager.categoryParam.page = 1;
		manager.categoryParam.flag = value;
		manager.getCategoryData();
	});
}

manager.itemItem = function() {
	$('.item_con').click(function() {
		$('.item_con').removeClass('select');
		$(this).addClass('select');
		if ($(this).html() == "管理员") {
			$('#manager_right').css('display', 'block');
			$('#category_right').css('display', 'none');
			manager.param.type = "manager";
		} else {
			$('#category_right').css('display', 'block');
			$('#manager_right').css('display', 'none');
			manager.param.type = "category";
			manager.categoryParam.page = 1;
			manager.categoryParam.flag = 1;
			manager.getCategoryData();
		}
	});
}

manager.init = function() {
	manager.itemItem();
	manager.getData();
	manager.categoryInit();
}

$(document).ready(function() {
	manager.init();
});